package com.example.demo.controller;

import com.example.demo.common.AppConstant;
import com.example.demo.common.HTTPResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Userinfo;
import com.example.demo.entity.vo.UserinfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description: 用户操作控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    // 用户注册功能
    @RequestMapping("/reg")
    public HTTPResult reg(Userinfo userinfo) {
        if(userinfo == null || !StringUtils.hasLength(userinfo.getUsername()) ||
                !StringUtils.hasLength(userinfo.getPassword())) {
            return HTTPResult.fail(-1, "非法参数");
        }
        Integer result = userService.insertUser(userinfo);
        return HTTPResult.success(result);
    }

    // 验证用户名和密码是否存在
    @RequestMapping("/login")
    public HTTPResult login(String username, String password, HttpServletRequest req) {
        if(!StringUtils.hasLength(username) ||
                !StringUtils.hasLength(password)) {
            return HTTPResult.fail(-1, "非法参数");
        }
        // 获取数据库信息
        Userinfo user = userService.getUserByName(username);
        if(user != null && user.getPassword().equals(password)) {
            // 验证用户身份通过
            // 返回前端前，隐藏敏感信息
            user.setPassword("");
            // 将用户存储到 session
            HttpSession session = req.getSession(true);
            session.setAttribute(AppConstant.USER_SESSION_KEY, user);
            return HTTPResult.success(user);
        }
        // 用户不存在，返回
        return HTTPResult.fail(0,"用户名或密码错误！", null);
    }

    // 获取用户的用户名与文章数量
    @RequestMapping("/showinfo")
    public HTTPResult showinfo(HttpServletRequest request) {
        UserinfoVO userinfoVO = new UserinfoVO();
        // 1. 获取当前登录用户
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null) {
            return HTTPResult.fail(-1, "非法请求");
        }
        // Spring 提供的深克隆方法
        BeanUtils.copyProperties(userinfo,userinfoVO);
        // 得到用户发表文章的总数
        int artCount = articleService.getArtCountByUid(userinfo.getId());
        userinfoVO.setArtCount(artCount);
        return HTTPResult.success(userinfoVO);
    }

    // 注销用户
    @RequestMapping("/logout")
    public HTTPResult logout(HttpSession session) {
        session.removeAttribute(AppConstant.USER_SESSION_KEY);
        return HTTPResult.success("1");
    }
}
